#include "MCU_include.h"

bit_type IIC1;

bit bIIC_ERROR;			// I2c communication error bit
vu8 PowerStep_buf,I2cError_Cnt=0;
vu8 dataID,data,cmd;
vu8 Undefine;
static u8 iic_Rx_Step=0;
static u8 iic_Tx_Step=0;

volatile ControlSet_Flag  	ControlSet_App;
volatile _FLAG 				FLAG;
volatile IH_Status_FLAG 		IH_Status_APP;
volatile _POWER_CTRL			POWER_CTRL;
volatile _DEBUGMODE_CTRL		Debug_Mode;

volatile _FG_IH_Status0  FG_IH_Status;//new.   myc

vu8		u8_PowerSetHB_buf=0,u8_PowerSetLB_buf=0;
vu8		u8_CheckSum;
/*vu8		u8_PowerMea_HB,u8_PowerMea_LB;*/	
vu8		u8_ctrl_set_rd,u8_power_set_h_rd,u8_power_set_l_rd;
vu8		u8_I2cError_Cnt=0;
vu8		u8_Commend=0;
vu8		IIC_Cnt = 0;



//---------------------	
void IIC_Init()
{
	SCL_C = 1;	//Input
	SDA_C = 1;
	
	SIM_SCK_SCL_PB7();
	SIM_SDI_SDA_RX_PB6();
	
//	_sclps = 0;	//SCL input source pin selection PA2
//	_sdaps = 0;	//SDA input source pin selection PA0

//_sclps = 1;	//SCL input source pin selection PB4
//_sdaps = 1;	//SDA input source pin selection PB0	
	
	_sima = C_DEVICE_ID;			//Slave	address 0x72	
	_simc0= 0b11000110;  		
	_simtoc=0b10011111; 		
	_usimf=0;
	_usime=1;
}


void IIC_Check(unsigned char Temp_Time_10msCnt)
{
	if ((_pwmon == 1) && (FG_IH_Status.flagBit.mode == 0))
	{
		if (++IIC_Cnt >= Temp_Time_10msCnt)
		{
			IIC_Cnt = 0;
			FG_IH_Status.flagBit.PWM_ON_OFF = 0;
		//	IH_Status_APP.flagBit.error = ERROR_I2C ;	//IIC error
			IGBT_OFF(PWM_F);
			PWM_F = d_PWM_F;			
		} 
	}	
}
void __attribute (( interrupt(0x1c) ))  I2c_ISR(void)//I2C Interrupt service routine
{
	 static u8 IIC_read_data[5];
	 static u8 Check_Sum = 0;	
//	 	uart_put_char('S');
//vu8 RX_AD_Iac,RX_AD_Vac,RX_Temp_PAN,RX_Temp_Coil,RX_Temp_IGBT,RX_PanDetPulseCnt;
	
	if(!_simtof)  // time out
	{
		if(_haas)   //_haas=1,address match trig interrupt	
		{			
			IIC_Cnt = 0 ;	
			if(_srw)  //srw=1:slave in transfer mode
			{		
				_htx=1;	
			
				_simd=cmd;						
			}
			else  //srw=0:slave in receive mode	
			{	  
				_htx=0;	
				_txak=0;
				_acc=_simd;  			
			}
		}
		else	//_haas=0,data trig interrupt
		{	
			if(_htx)  	///htx=1:slave in write state;	
			{		  	
				if(_rxak)  //rxak=1:master stop receiving next byte,master releases scl bus
				{		
					_htx=0; _txak=0;
					_acc=_simd;  	
				}	
				else //rxak=0:master wants to receive next byte;	
				{
					//Receive clear
					iic_Rx_Step=0;
					//	cmd = 0x01 ;
						
					if(C_OPERATION_CMD == cmd)  
					{
						switch(iic_Tx_Step++)
						{
							//----------------------------		
							case 0:	
					
									_simd=AD_Temp_IGBT;							
									Check_Sum=AD_Temp_IGBT; 
							
							//		  _simd = IIC_read_data[1] ;                        	 
							break ;	
							//----------------------------		
							case 1:		
									_simd=AD_Temp_PAN ;		
									Check_Sum=Check_Sum+AD_Temp_PAN ;   
					
								                         	
							break ;	
							//----------------------------
							case 2:	
									_simd= AD_TEMP_Coil;		
									Check_Sum=Check_Sum+AD_TEMP_Coil;
									
					
								
							break ;	
							//----------------------------							
							case 3:											
									_simd =  IH_Status_APP.m_data;			
																			
									Check_Sum=Check_Sum+ IH_Status_APP.m_data;	
					
							break ;
							//----------------------------		
							case 4:	
									_simd = C_NC;   			//0
									Check_Sum=Check_Sum+C_NC;
							
					
							break ;
							//----------------------------		
							case 5:
									_simd=Check_Sum;	//Check_Sum   							
									iic_Tx_Step=0;
									Check_Sum = 0;
							
			
												
							break ;
							//----------------------------
							default :
									iic_Tx_Step=0;	
									Check_Sum = 0;
							break;
						}
					}
					else if(C_CALIBRATION_CMD == cmd)
					{
						GCC_NOP();
					}
					
					//------------------------------------------------------
					else if(C_INITSETTING_CMD == cmd)
					{
						GCC_NOP();	
					}
					//------------------------------------------------------
					
					else if(C_DEBUG_CMD == cmd)
					{
							GCC_NOP();
					}
				}				
			}
			else   //htx=0:slave in read state
			{
				iic_Tx_Step = 0;
				{
					switch(iic_Rx_Step++)
					{			
						case 0: 
								//iic_Rx_Step++;
								IIC_read_data[0]=_simd;	//Receive the 1th byte	
								_txak=0;	
						break;
						//----------------------------		
						case 1://Receive the 2nd byte	 
								IIC_read_data[1]=_simd;//	
								_txak=0;				
						break ;
						//----------------------------
						case 2://Receive 3rd byte    
								IIC_read_data[2]=_simd;//		
								_txak=0;
						break ;						
						//----------------------------
						case 3://Receive the 4th byte   
								IIC_read_data[3]=_simd;		
								_txak=0;
						break ;						
						//----------------------------
						case 4://Receive the 5th byte  (Checksum)
							IIC_read_data[4]=_simd;			
							_txak=1;			
							Check_Sum=IIC_read_data[1]+IIC_read_data[2]+IIC_read_data[3];
							if(IIC_read_data[4]==Check_Sum)
							{
								cmd = IIC_read_data[0];	
								switch(cmd)
								{
									case C_OPERATION_CMD: 			
											ControlSet_App.m_data =IIC_read_data[1];		
											u8_PowerSetHB_buf =IIC_read_data[2];     //											
											u8_PowerSetLB_buf=IIC_read_data[3];	
									break;
									//------------------------
									case C_CALIBRATION_CMD:			

									break;
									//------------------------
									case C_INITSETTING_CMD:			

									break;
									//------------------------
									case C_DEBUG_CMD:					

									break;
									//------------------------
								}
								I2cError_Cnt=0;
						
								Check_Sum = 0;	
							}
							else
							{
							//	_txak=1;    // NO ACK
							}
							Check_Sum = 0;
							iic_Rx_Step=0;
						break ;
						//----------------------------
						default :
							iic_Rx_Step=0;
						break ;							
					}
				}	
			}
				FG_IH_Status.flagBit.IIC_CF = 1 ;
		}	
	}	
	else  //Timeout occurred
	{
		_simtof=0;
		_simtoen=1;
	}
	_usimf=0;  //Clear the interrupt flag bit
}


//******************************************************//
//********************IIC received power processing********************//
//input1:
//output:
//******************************************************//
//******************************************************// 
void Get_PwoerSet_from_IIC()
{
	static	u16 u16_PowerSetBUFF0 , u16_PWM_F_BUFF =0;
	
	if(FG_IH_Status.flagBit.IIC_CF == 1)	
		{
			FG_IH_Status.flagBit.IIC_CF = 0;
			BuzzerProcess();
		
			
			if (FG_IH_Status.flagBit.mode == 0)		
			{			
				u16_PowerSet = u8_PowerSetHB_buf<<8 | u8_PowerSetLB_buf;		//Save the power settings
				
				Voletage_PowerLimit();	//Limit power when voltage is abnormal
					
				IGBT_OTP_LimitPower();
				
				u16_PowerSet_Target_H = u16_PowerSet/50;
				u16_PowerSet_Target_L = 49*u16_PowerSet_Target_H;	//Power tolerance lower limit 0.98*Power_Set	
				u16_PowerSet_Target_H = 51*u16_PowerSet_Target_H;	//Power tolerance upper limit 1.02*Power_Set
				
			if(	u16_PowerSetBUFF0 != u16_PowerSet) 
			{
				u16_PowerSetBUFF0 = u16_PowerSet;
				FG_IH_Status.flagBit.PWOER_SET_CHANGE = 1;
				OVP2_Over_I_flag =0;
				
				u16_PWM_F_BUFF = _pwmph;
				u16_PWM_F_BUFF = (u16_PWM_F_BUFF << 8)  + (_pwmpl);
				PWM_F = PWM_SF/u16_PWM_F_BUFF;		
				POWER_CTRL.flagBit.Power_OK = 0;
			}
				
			//********************Debug manual frequency modulation mode ********************//	
				if (u16_PowerSet == 0x55)
				{
					u16_PowerSet =0;
					if (PWM_F >= 30000)	PWM_F = PWM_F - 500;
					else if (PWM_F >= 24000)	PWM_F = PWM_F - 100;
					else PWM_F = PWM_F - 20;
					if (PWM_F < MIN_FRE)
					PWM_F = MIN_FRE;
					PWM_Update(PWM_F);
				}
				else if (u16_PowerSet == 0xaa)
				{
					u16_PowerSet=0;
					if (PWM_F >= 30000)	PWM_F = PWM_F + 500;
					else if (PWM_F >= 24000)	PWM_F = PWM_F + 100;
					else PWM_F = PWM_F + 20;
					if (PWM_F > MAX_FRE)
					PWM_F = MAX_FRE;
					PWM_Update(PWM_F);
				}	
				if(ControlSet_App.flagBit.on_off==1 )			
				{
					FG_IH_Status.flagBit.Machine_ON_OFF = 1;
		
				}
					        	
				else
				{
					PowerOFF();
				}
			


			}
		}
}